# coding=utf-8
from random import Random
import sys
import os
pwd = os.path.realpath(__file__)
workdir = pwd[:pwd.rfind("/")]
workdir = workdir[:workdir.rfind("/")]
sys.path.append(workdir + "/SourseCodes")
from MimicryHoneypot import *
banner = "Hello.Welcome to pseudo honeypot controller."
length = 50
print("-" * length)
print(banner)
print("-" * length)
honeypot = MimicryHoneypot(mode='real')
name_list = {
    "ip":"ip地址",
    "anonymous_enable":"是否开启匿名登录",
    "anon_upload_enable":"匿名用户是否开启上传",
    "no_anon_password":"匿名用户是否要求输入密码",
    "banner_file":"欢迎语",
    "chroot_local_user":"本地用户是否有文件夹限制",
    "anon_root":"匿名用户根目录",
    "anon_other_write_enable":"匿名用户其他写权限",
    "anon_mkdir_write_enable":"匿名用户可创建文件夹",
    "ftp_username":"匿名登录用户名",
    "listen_port":"监听端口"
}
def aligns(string,length=20):
    difference = length - len(string)  # 计算限定长度为20时需要补齐多少个空格
    if difference == 0:  # 若差值为0则不需要补
        return string
    elif difference < 0:
        print('错误：限定的对齐长度小于字符串长度!')
        return None
    new_string = ''
    space = '　'
    for i in string:
        codes = ord(i)  # 将字符转为ASCII或UNICODE编码
        if codes <= 126:  # 若是半角字符
            new_string = new_string + chr(codes+65248) # 则转为全角
        else:
            new_string = new_string + i  # 若是全角，则不转换
    return new_string + space*(difference)  # 返回补齐空格后的字符串
while(True):
    command = input("请输入命令:\n\t1:触发演化   2:显示当前状态   3:退出\n输入：")
    if command == "1":
        os.system("sudo ./fetchConfig.sh")
        honeypot.pseudo_step('/etc/vsftpd/vsftpd_configs.pkl')
        print("-" * length)
        print("演化后的状态为:")
        ip,configs = honeypot.getCurrentConfig()
        print(aligns("特征") + aligns("值"))
        print("-" * length)
        print(aligns("IP地址") + aligns(ip))
        for each in configs:
            print(aligns(name_list[each]) + aligns(configs[each]))
        print("-" * length)
    elif command == "2":
        print("-" * length)
        print("当前状态为:")
        ip,configs = honeypot.getCurrentConfig()
        pkts = honeypot.pkt_num
        print("-" * length)
        print(aligns("当前攻击流量") + aligns(str(pkts)))
        print("-" * length)
        print(aligns("特征") + aligns("值"))
        print("-" * length)
        print(aligns("IP地址") + aligns(ip))
        for each in configs:
            print(aligns(name_list[each]) + aligns(configs[each]))
        print("-" * length)
    elif command == "3":
        print("-" * length)
        print("再见！")
        print("-" * length)
        break
    else:
        print("-" * length)
        print("无效命令")
        print("-" * length)

